package com.mall.user.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.mall.user.ILoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.*;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author: lds
 * @clsss: ILoginServiceImpl
 * @description:
 */
@Transactional(rollbackFor = Exception.class)
@Slf4j
@Service
@Component
public class ILoginServiceImpl implements ILoginService {


    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Override
    public CheckAuthResponse validToken(CheckAuthRequest checkAuthRequest) {
        CheckAuthResponse checkAuthResponse = new CheckAuthResponse();
        String token = checkAuthRequest.getToken();
        try {
            String info = JwtTokenUtils.builder().token(token).build().freeJwt();
            ObjectMapper objectMapper = new ObjectMapper();
            UsernameIdToken usernameIdToken = objectMapper.readValue(info, UsernameIdToken.class);
            String username = usernameIdToken.getUsername();
            checkAuthResponse.setUserinfo(usernameIdToken);
            checkAuthResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            checkAuthResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        }catch (Exception e) {
            checkAuthResponse.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED.getCode());
            checkAuthResponse.setMsg(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
        }

        return checkAuthResponse;
    }

    @Override
    public UserLoginResponse2 selectUser(CheckAuthRequest checkAuthRequest) {
        UserLoginResponse2 userLoginResponse = new UserLoginResponse2();

        try {
            checkAuthRequest.requestCheck();
            String token = checkAuthRequest.getToken();
            String userInfo = JwtTokenUtils.builder().token(token).build().freeJwt();
            ObjectMapper objectMapper = new ObjectMapper();
            UsernameIdToken usernameIdToken = objectMapper.readValue(userInfo, UsernameIdToken.class);
            String username = usernameIdToken.getUsername();
            Example example = new Example(Member.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("username", username);
            List<Member> members = memberMapper.selectByExample(example);
            Member member = members.get(0);
            UserDto userDto = new UserDto();
            userDto.setUid(member.getId());
            userDto.setFile(member.getFile());
            userLoginResponse.setUserDto(userDto);
            userLoginResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            userLoginResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        }catch (Exception e) {

            userLoginResponse.setCode(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getCode());
            userLoginResponse.setMsg(SysRetCodeConstants.REQUEST_CHECK_FAILURE.getMessage());
        }
        return userLoginResponse;
    }

    @Override
    public UserVerifyResponse userVerify(UserVerifyRequest userVerifyRequest) {
        log.error("ILoginServiceImpl.userVerify userVerifyRequest :"+userVerifyRequest);
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();

        try {
            String userName = userVerifyRequest.getUserName();
            String uuid = userVerifyRequest.getUuid();
            Example example = new Example(UserVerify.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("username",userName)
                    .andEqualTo("uuid",uuid)
                    .andEqualTo("cisVerify","N")
                    .andEqualTo("cisExpire","N");
            List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
            if (userVerifies.size() == 1) {

                UserVerify userVerify = userVerifies.get(0);
                userVerify.setCisVerify("Y");
                userVerify.setCisExpire("Y");
                Example example2 = new Example(UserVerify.class);
                example2.createCriteria().andEqualTo("id",userVerify.getId());
                userVerifyMapper.updateByExampleSelective(userVerify,example2);
                Member member = new Member();
                member.setIsVerified("Y");
                Example exampleMember = new Example(Member.class);
                exampleMember.createCriteria().andEqualTo("username",userVerify.getUsername());
                memberMapper.updateByExampleSelective(member,exampleMember);
                userVerifyResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
                userVerifyResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            }else if (userVerifies.size() == 0){

                userVerifyResponse.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR2.getMessage());
                userVerifyResponse.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR2.getCode());
            }else {

                userVerifyResponse.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR3.getMessage());
                userVerifyResponse.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR3.getCode());
            }
        }catch (Exception e) {
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR3.getMessage());
            userVerifyResponse.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR3.getCode());
        }

        return userVerifyResponse;
    }




}
